local XBarGuildButtons={};
local start=1;
local max=9;
 
function XBarGuild_OnEvent(this,event)
-- ChatFrame1:AddMessage("XBar-Guild : "..event);
	if (event=="LOADING_END") then
		this:RegisterEvent("UPDATE_GUILD_MEMBER");	
		this:RegisterEvent("UPDATE_GUILD_MEMBER_INFO");	
		this:RegisterEvent("GUILD_INVITE_REQUEST");	
		this:RegisterEvent("GUILD_KICK");	
		this:RegisterEvent("GUILDINVITE_SELF");	
		this:RegisterEvent("GUILD_RENAME_OPEN");	
	end

-- Output
	local usrtxt={[1]=XBar["GuildV1"],[2]=XBar["GuildV2"]};
	local output="";
	for i=1,2 do
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[GNAME%]",tostring(XBarGuild_Info("GuildName")));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[GLVL%]",tostring(XBarGuild_Info("GuildLevel")));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[GLEADER%]",tostring(XBarGuild_Info("GuildLeader")));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[ONLINE%]",tostring(XBarGuild_Info("OnlineCount")));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[MEMBER%]",tostring(GetNumGuildMembers()));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[MAX%]",tostring(XBarGuild_Info("MaxMember")));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[RIGHT%]",tostring(XBarGuild_Info("CanRecruit")));
	end
	if (XBar["GuildT1"]==true) then output=usrtxt[1]; end
	if (XBar["GuildT2"]==true) then
		if (XBar["GuildT1"]==true) then output=output.."\n"..usrtxt[2]; else output=usrtxt[2]; end
	end
	XBarGuild_F_Text:SetText(output);
-- EoL
end

function XBarGuild_Info(v1,v2,v3,v4)
	if (GetNumGuildMembers()==0) then return "nil"; end
	local OnlineCount=0;
	local GuildName,GuildLeader,CanRecruit,IsGuild,MaxMember,GuildScore,GuildNote,GuildIntro,IsLeader,IsBBS,IsOpen,GuildLevel,GuildHome,OpenVisit=GetGuildInfo();
	if (v1=="GuildName") then return GuildName; end
	if (v1=="GuildLeader") then return GuildLeader; end
	if (v1=="CanRecruit") then if (CanRecruit==true) then return XLng["Ttip"]["Guild9"]; else return XLng["Ttip"]["Guild10"]; end; end
	if (v1=="IsGuild") then return IsGuild; end
	if (v1=="MaxMember") then return MaxMember; end
	if (v1=="GuildScore") then return GuildScore; end
	if (v1=="GuildNote") then return GuildNote; end
	if (v1=="GuildIntro") then return GuildIntro; end
	if (v1=="IsLeader") then return IsLeader; end
	if (v1=="IsBBS") then return IsBBS; end
	if (v1=="IsOpen") then return IsOpen; end
	if (v1=="GuildLevel") then return GuildLevel; end
	if (v1=="GuildHome") then return GuildHome; end
	if (v1=="OpenVisit") then return OpenVisit; end
	if (v1=="OnlineCount") then
		for i=1,GetNumGuildMembers() do
			local _,_,_,_,_,_,_,_,_,_,On,_,_,_=GetGuildRosterInfo(i);
			if (On) then if (On==true) then OnlineCount=OnlineCount+1; end; end
		end
		return OnlineCount;
	end
end

function XBarGuild_OnClick(key,wheel)
	local scroll=(wheel and 1) or false;
	if (key=="LBUTTON") then
		XBarGuildButtons={};
		local count=0;
		for i=1,GetNumGuildMembers() do
			local Name,Rank,MC,ML,SC,SL,_,_,DBID,Title,Online,LogTime,Zone,Note=GetGuildRosterInfo(i);
			if (Online) then
				if (Online==true) then
					count=count+1;
					XBarGuildButtons[count]={
						icon=XBarGuild_ClassIcon(MC),
						CharName=Name,
						GetText=function() return "|cff"..XBarGuild_ClassColor(MC)..Name.."|r"; end,
						GetTooltip=function()
							 info="";
							 info=info.."|cffC5B358"..XLng["Ttip"]["MC"].."|r |cff"..XBarGuild_ClassColor(MC)..MC.."|r ("..ML..")\n";
							 info=info.."|cffC5B358"..XLng["Ttip"]["SC"].."|r |cff"..XBarGuild_ClassColor(SC)..SC.."|r ("..SL..")\n";
							 info=info.."|cffC5B358"..XLng["Ttip"]["Loc"].."|r "..Zone.."\n";
							 info=info.."\n";
							 info=info.."|cff857318"..XLng["Ttip"]["Rank"].."|r |cff767676"..Rank.."|r\n";
							 info=info.."|cff857318"..XLng["Ttip"]["DBID"].."|r |cff767676"..XBar_SetDecimals(DBID).."|r\n";
							 if (Note) then if (Note~="") then info=info.."|cff857318"..XLng["Ttip"]["Note"].."|r |cff767676"..tostring(Note).."|r\n"; end; end
							 info=info.."\n";
							 info=info.."|cff00B2E5"..XLng["Ttip"]["LMOUSE_WSP"].."|r\n";
							 info=info.."|cff00B2E5"..XLng["Ttip"]["RMOUSE_INV"].."|r\n";
							 info=info.."|cff00B2E5"..XLng["Ttip"]["SCROLL_ON"].."|r\n";
							 return info; end,
						OnClick=function(this,key) XBarGuild_OnSelect(this,key); end,
						OnScroll=function(delta) XBarGuild_Scroll(delta); end,
					};
				end
			end
		end
		if (start>1) then for i=1,start-1 do table.remove(XBarGuildButtons,1); end; end
		while #XBarGuildButtons>max do table.remove(XBarGuildButtons); end
		XBar_PopupMenu.Buttons=XBarGuildButtons;
		XBar_PopupMenu_Toggle("XBarGuild",0,32,scroll);
	end
end

function XBarGuild_Scroll(delta)
	if (delta>0) then if (start>1) then start=start-1; end; end
	if (delta<0) then if (start<XBarGuild_Info("OnlineCount")-(max-1)) then start=start+1; end; end
	XBarGuild_OnClick("LBUTTON",true);
end

function XBarGuild_OnSelect(this,key)
	if (key=="LBUTTON") then ChatFrame_SendTell(XBar_PopupMenu.Buttons[this:GetID()].CharName); end
	if (key=="RBUTTON") then InviteByName(XBar_PopupMenu.Buttons[this:GetID()].CharName); end
end

function XBarGuild_ClassIcon(class)
	if (not class) then return nil;
	elseif (class==GetSystemString("SYS_CLASSNAME_WARRIOR")) then return "Interface\\TargetFrame\\TargetFrameIcon-Warrior";
	elseif (class==GetSystemString("SYS_CLASSNAME_RANGER")) then return "Interface\\TargetFrame\\TargetFrameIcon-Ranger";
	elseif (class==GetSystemString("SYS_CLASSNAME_THIEF")) then return "Interface\\TargetFrame\\TargetFrameIcon-Thief";
	elseif (class==GetSystemString("SYS_CLASSNAME_MAGE")) then return "Interface\\TargetFrame\\TargetFrameIcon-Mage";
	elseif (class==GetSystemString("SYS_CLASSNAME_AUGUR")) then return "Interface\\TargetFrame\\TargetFrameIcon-Augur";
	elseif (class==GetSystemString("SYS_CLASSNAME_KNIGHT")) then return "Interface\\TargetFrame\\TargetFrameIcon-Knight";
	elseif (class==GetSystemString("SYS_CLASSNAME_WARDEN")) then return "Interface\\TargetFrame\\TargetFrameIcon-Warden";
	elseif (class==GetSystemString("SYS_CLASSNAME_DRUID")) then return "Interface\\TargetFrame\\TargetFrameIcon-Druid";
	else return "Interface\\TargetFrame\\TargetFrameIcon-Druid"; end
end
function XBarGuild_ClassColor(class)
	if (not class) then return nil;
	elseif (class==GetSystemString("SYS_CLASSNAME_WARRIOR")) then return "FF0000";
	elseif (class==GetSystemString("SYS_CLASSNAME_RANGER")) then return "A5D603";
	elseif (class==GetSystemString("SYS_CLASSNAME_THIEF")) then return "00D6C5";
	elseif (class==GetSystemString("SYS_CLASSNAME_MAGE")) then return "FFC000";
	elseif (class==GetSystemString("SYS_CLASSNAME_AUGUR")) then return "288CEC";
	elseif (class==GetSystemString("SYS_CLASSNAME_KNIGHT")) then return "FFFF48";
	elseif (class==GetSystemString("SYS_CLASSNAME_WARDEN")) then return "0CF374";
	elseif (class==GetSystemString("SYS_CLASSNAME_DRUID")) then return "8B35FF";
	else return "FFFFFF"; end
end
